package com.sihan.swvp.common;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
@SuppressWarnings("unused")
public class ReportExcelCellStyle 
{
	//表头格式
	public HSSFCellStyle titleCellStyle;
	public HSSFCellStyle headCellStyle;
	public HSSFCellStyle groupHeadStyle;
	//普通文本位置格式
	public HSSFCellStyle rightStyle;
	public HSSFCellStyle leftStyle;
	public HSSFCellStyle centerStyle;
	//数字位置格式
	public HSSFCellStyle rightNumberStyle;
	public HSSFCellStyle leftNumberStyle;
	public HSSFCellStyle centerNumberStyle;
	
	public HSSFCellStyle rightIntegerStyle;
	public HSSFCellStyle leftIntegerStyle;
	public HSSFCellStyle centerIntegerStyle;
	
	public ReportExcelCellStyle(HSSFWorkbook workbook)
	{
		titleCellStyle = initTitleCellStyle(workbook);
		//表头样式初始化
		headCellStyle = initHeadCellStyle(workbook);
		
		//普通文本样式初始化
		rightStyle = initRightStyle(workbook);
		leftStyle = initLeftStyle(workbook);
		centerStyle = initCenterStyle(workbook);
		
		//精确两位数字样式初始化
		rightNumberStyle = initRightNumberStyle(workbook);
		leftNumberStyle = initLeftNumberStyle(workbook);
		centerNumberStyle = initCenterNumberStyle(workbook);
		
		//整数数样式初始化
		rightIntegerStyle = initRightIntegerStyle(workbook);
		leftIntegerStyle = initLeftIntegerStyle(workbook);
		centerIntegerStyle = initCenterIntegerStyle(workbook);
	}
	
	private HSSFCellStyle initTitleCellStyle(HSSFWorkbook workbook)
	{
		HSSFPalette palette = ((HSSFWorkbook)workbook).getCustomPalette();
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
		Font font = workbook.createFont();
		font.setFontHeightInPoints((short)14);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
		cellStyle.setFont(font);
		
		return cellStyle;
	}
	
	private HSSFCellStyle initHeadCellStyle(HSSFWorkbook workbook)
	{
		HSSFPalette palette = ((HSSFWorkbook)workbook).getCustomPalette();
		palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 236, (byte) 237, (byte) 238);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFillForegroundColor(HSSFColor.LIME.index); 
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
		
		return cellStyle;
	}
	
	private HSSFCellStyle initGroupHeadCellStyle(HSSFWorkbook workbook)
	{
		HSSFPalette palette = ((HSSFWorkbook)workbook).getCustomPalette();
		palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 236, (byte) 237, (byte) 238);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFillForegroundColor(HSSFColor.LIME.index); 
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
		
		return cellStyle;
	}
	
	private HSSFCellStyle initRightStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);  
		
		return cellStyle;
	}
	
	private HSSFCellStyle initLeftStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);  
		
		return cellStyle;
	}
	
	private HSSFCellStyle initCenterStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
		
		return cellStyle;
	}
	
	private HSSFCellStyle initRightIntegerStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);  
		cellStyle.setDataFormat(workbook.createDataFormat().getFormat("0"));   
		
		return cellStyle;
	}
	
	private HSSFCellStyle initLeftIntegerStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); 
		cellStyle.setDataFormat(workbook.createDataFormat().getFormat("0"));   
		
		return cellStyle;
	}
	
	private HSSFCellStyle initCenterIntegerStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
		cellStyle.setDataFormat(workbook.createDataFormat().getFormat("0"));   
		
		return cellStyle;
	}
	
	private HSSFCellStyle initRightNumberStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);  
		cellStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00"));   
		
		return cellStyle;
	}
	
	private HSSFCellStyle initLeftNumberStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); 
		cellStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00"));   
		
		return cellStyle;
	}
	
	private HSSFCellStyle initCenterNumberStyle(HSSFWorkbook workbook)
	{
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
		cellStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00"));   
		
		return cellStyle;
	}
	
	public void setHeadCellStyle(Cell cell)
	{
		cell.setCellStyle(this.headCellStyle);
	}
	
	
	public void setDataCellStyle(HSSFCell cell,String viewPosition,String viewType)
	{
		String value = cell.getStringCellValue();
		if("".equals(value)){
			return ;
		}
		
		if("right".equals(viewPosition))
		{
			if("text".equals(viewType))
			{
				cell.setCellStyle(this.rightStyle);
			}
			else if("num".equals(viewType))
			{
				
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum)
				{
					cell.setCellValue(Double.parseDouble(value));
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.rightIntegerStyle);
			}
			else if("price".equals(viewType))
			{
				cell.setCellStyle(this.leftNumberStyle);
			}
			else if("price_wy".equals(viewType))
			{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum&&!value.equals(""))
				{
					cell.setCellValue(Double.parseDouble(value)/10000);
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.rightNumberStyle);
			}
			else if("point".equals(viewType))
			{
				boolean isNum = value.matches("^[+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum)
				{
					cell.setCellValue(Double.parseDouble(value)*100);
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.rightNumberStyle);
			}else{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum&&!value.equals(""))
				{
					cell.setCellValue(Double.parseDouble(value));
					cell.setCellStyle(this.rightNumberStyle);
				}
			}
		}
		else if("left".equals(viewPosition))
		{
			if("text".equals(viewType))
			{
				cell.setCellStyle(this.leftStyle);
			}
			else if("num".equals(viewType))
			{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum)
				{
					cell.setCellValue(Double.parseDouble(value));
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.rightIntegerStyle);
			}
			else if("price_wy".equals(viewType))
			{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum)
				{
					cell.setCellValue(Double.parseDouble(value)/10000);
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.leftNumberStyle);
			}
			else if("point".equals(viewType))
			{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum)
				{
					cell.setCellValue(Double.parseDouble(value)*100);
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.leftNumberStyle);
			}else{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum&&value.contains("."))
				{
					cell.setCellValue(Double.parseDouble(value));
					cell.setCellStyle(this.rightIntegerStyle);
				}
				else if(isNum){
					cell.setCellValue(Double.parseDouble(value));
					cell.setCellStyle(this.rightNumberStyle);
				}
			}
		}
		else if("center".equals(viewPosition))
		{
			if("text".equals(viewType))
			{
				cell.setCellStyle(this.centerStyle);
			}
			else if("num".equals(viewType))
			{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum)
				{
					cell.setCellValue(Double.parseDouble(value));
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.rightIntegerStyle);
			}
			else if("price_wy".equals(viewType))
			{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum)
				{
					cell.setCellValue(Double.parseDouble(value)/10000);
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.centerNumberStyle);
			}
			else if("point".equals(viewType))
			{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum&&!value.equals(""))
				{
					cell.setCellValue(Double.parseDouble(value)*100);
				}
				else
				{
					cell.setCellValue("--");
				}
				cell.setCellStyle(this.centerNumberStyle);
			}
			else{
				boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
				if(isNum&&value.contains("."))
				{
					cell.setCellValue(Double.parseDouble(value));
					cell.setCellStyle(this.rightIntegerStyle);
				}
				else if(isNum){
					cell.setCellValue(Double.parseDouble(value));
					cell.setCellStyle(this.rightNumberStyle);
				}
			}
		}
		else{
			boolean isNum = value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
			if(isNum&&!value.contains("."))
			{
				cell.setCellValue(Double.parseDouble(value));
				cell.setCellStyle(this.rightIntegerStyle);
			}
			else if(isNum){
				cell.setCellValue(Double.parseDouble(value));
				cell.setCellStyle(this.rightNumberStyle);
			}
		}
	}
	
}
